<script setup lang='ts'>
import { reactive, ref, onMounted } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { CircleCloseFilled, Plus } from '@element-plus/icons-vue'
import type { IDeptHs } from '../../types/dept'

import http from '../../api/http'

//props  读取父组件传递的 属性值 （只读）
// const props = defineProps(['dialogVisible'])
// const emit = defineEmits(['change'])

const dialogVisible = defineModel()
const emit = defineEmits<{ (e: 'refresh'): void }>()

const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<IDeptHs>({
      deptID:0,
    deptName:'',
    remark:'',
    type:1,
    lastTime:new Date()
})




//#endregion

//表单验证
const rules = reactive<FormRules<IDeptHs>>({
    deptName: [
        { required: true, message: '请输入姓名!', trigger: 'blur' },
        { min: 2, max: 10, message: '长度在3-10个字符之间!', trigger: 'blur' },
    ]
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {

        if (valid) {
            //axios 提交后台

            http.post('Dept', ruleForm).then(res => {
                if (res.code == '1') {
                    ElMessage({
                        message: res.msg,
                        type: 'success',
                    })
                    dialogVisible.value = false
                    emit('refresh')
                }
                else {
                    ElMessage({
                        message: res.msg,
                        type: 'error',
                    })
                }

            }).catch(err => {
                console.log('err: ', err);
            })


            //关闭弹窗 并刷新列表页面数据
            dialogVisible.value = false
            emit('refresh')
        } else {
            console.log('error submit!', fields)
        }
    })
}


</script>

<template>

    <el-dialog v-model="dialogVisible" :show-close="false" width="500" center>
        <template #header="{ close, titleId, titleClass }">
            <div class="my-header">
                <h4 :id="titleId" :class="titleClass"></h4>
                <el-button type="danger" @click="close">
                    <el-icon class="el-icon--left">
                        <CircleCloseFilled />
                    </el-icon>
                    关闭
                </el-button>
            </div>
        </template>

        <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">
           
            <el-form-item label="姓名" prop="deptName" >
                <el-input v-model="ruleForm.deptName" placeholder="请输入部门名称" />
            </el-form-item>
            <el-form-item label="描述" prop="remark">
                <el-input v-model="ruleForm.remark" />
            </el-form-item>
        </el-form>


        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    确定
                </el-button>
            </div>
        </template>
    </el-dialog>

</template>


<style scoped>
.my-header {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    gap: 16px;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 128px;
    height: 128px;
    line-height: 128px;
    text-align: center;
}

.avatar {
    width: 128px;
    height: 128px;
    display: block;
}
</style>